博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
大项目之网上书城(十一)——前台完成
阅读量:5109 次
发布时间:2019-06-13

本文共 19248 字,大约阅读时间需要 64 分钟。

目录

大项目之网上书城(十一)——前台完成

主要改动

昨天的问题解决了,用户界面一分为三,原本的用户页面变成了现在的浏览历史界面,并多了一个修改信息界面和收货地址设置界面。同时,购物车和订单的代码完善,之前的bug进行修改。前台显示算是完成了。还差管理员的管理界面,以及填充数据库了。哦,对了,我今天新加了一个表,专门用来存收货地址,和用户表是n对1的。

新增代码

1.head.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"    pageEncoding="utf-8"%><%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>Insert title here

效果图

1513080-20190624235612954-1920824849.png

1513080-20190624235707347-1310910192.png

2.changeinfo.jsp

如果不填原密码的话,就代表不修改密码。

<%@page import="cn.edu.bdu.mc.beans.User"%><%@page import="cn.edu.bdu.mc.utils.CookieUtil"%><%@ page language="java" contentType="text/html; charset=utf-8"    pageEncoding="utf-8"%><%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>个人信息
欢迎您,${user.username } 
先生
女生
退出登录
  • 请输入有效的邮箱地址
  • 昵称请设置4~20位字符
  • 此项为空则不修改密码
  • 密码请设置4~20位字符

效果图

1513080-20190624235642968-1928818975.png

3.shou.jsp

有添加收货地址,设为默认地址的功能。订单时会默认选择默认地址。地址为空时添加会自动设为默认地址,否则是普通地址。

<%@page import="java.util.List"%><%@page import="cn.edu.bdu.mc.daos.impls.ShouhuoDaoImpl"%><%@page import="cn.edu.bdu.mc.daos.ShouhuoDao"%><%@page import="cn.edu.bdu.mc.beans.Shouhuo"%><%@page import="cn.edu.bdu.mc.beans.User"%><%@page import="cn.edu.bdu.mc.utils.CookieUtil"%><%@ page language="java" contentType="text/html; charset=utf-8"    pageEncoding="utf-8"%><%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>收货地址
欢迎您,${user.username } 
先生
女生
退出登录
<% User user = (User)session.getAttribute("user"); ShouhuoDao shouhuoDao = new ShouhuoDaoImpl(); Shouhuo shouhuo_default = shouhuoDao.findShouhuoById(user.getUser_id()); List
shouhuoList = shouhuoDao.findAllOtherShouhuoById(user.getUser_id()); %>

默认地址

<% if(shouhuo_default==null){ out.print("无"); }else{ %>
姓名 电话 地址
<%=shouhuo_default.getName() %> <%=shouhuo_default.getPhone() %> <%=shouhuo_default.getAddress() %>
<%} %>

其他地址

<% if(shouhuoList.isEmpty()){ out.print("无"); }else{ %>
<% for(Shouhuo shouhuo:shouhuoList){ %>
<%} %>
姓名 电话 地址 操作
<%=shouhuo.getName() %> <%=shouhuo.getPhone() %> <%=shouhuo.getAddress() %> 设为默认
<%} %>

效果图

1513080-20190624235742656-782528440.png

4.changeinfo.js

function checkEmail() {         // 验证邮箱    var regex = /^[\w-]+@([\w-]+\.)+[a-zA-Z]{2,4}$/;    var value =$("#email").val();    var msg = "";    if (!value)        msg = "邮箱必须填写:";    else if (!regex.test(value))        msg = "邮箱格式不合法:";    $("#emailMsg").html(msg);    $("#emailMsg").css("display","block");    $("#text1").css("display","none");    $("#emailS").css("display","none");    if(msg==""){        $("#emailMsg").css("display","none");        $("#text1").css("display","none");        $("#emailS").css("display","block");            }    return msg == "";}function checkUsername() {      // 验证用户名    var regex = /^[a-zA-Z_]\w{3,20}$/;  // 字母数字下划线1到10位, 不能是数字开头    var value = $("#username").val();// 获取usernameObj中的文本    var msg = "";                       // 最后的提示消息, 默认为空    if (!value)                         // 如果用户名没填, 填了就是一个字符串可以当作true, 没填的话不论null或者""都是false        msg = "用户名必须填写:";           // 改变提示消息    else if (!regex.test(value))        // 如果用户名不能匹配正则表达式规则        msg = "用户名不合法:";            // 改变提示消息    $("#usernameMsg").html(msg);        // 将提示消息放入SPAN    $("#usernameMsg").css("display","block");//让文字显示出来。    $("#text2").css("display","none");    $("#usernameS").css("display","none");    if(msg==""){        $("#usernameMsg").css("display","none");        $("#text2").css("display","none");        $("#usernameS").css("display","block");         }    return msg == "";                   // 如果提示消息为空则代表没出错, 返回true}function checkYuan(){    var value = $("#yuan").val();    $.post("/bookstore/MiMa",function(data){        if(data==value){            $("#yuanMsg").css("display","none");            $("#text4").css("display","none");            $("#yuanS").css("display","block");         }else{            $("#yuanMsg").css("display","block");            $("#text4").css("display","none");            $("#yuanS").css("display","none");        }    });}function checkPassword() {      // 验证密码    var regex = /^.{4,20}$/;            // 任意字符, 6到16位    var value =$("#password").val();    var msg = "";    if (!value)        msg = "密码必须填写:";    else if (!regex.test(value))        msg = "密码不合法:";    $("#passwordMsg").html(msg);    $("#passwordMsg").css("display","block");//让文字显示出来。    $("#text3").css("display","none");    $("#passwordS").css("display","none");    if(msg==""){        $("#passwordMsg").css("display","none");        $("#text3").css("display","none");        $("#passwordS").css("display","block");         }    return msg == "";}function checkConfirm() {       // 验证确认密码    var passwordValue = $("#password").val();    var confirmValue = $("#repassword").val();    var msg = "";    if(!confirmValue){        msg = "确认密码必须填写";    }    else    if (passwordValue != confirmValue){        msg = "密码必须保持一致";    }    $("#confirmMsg").html(msg);    $("#confirmMsg").css("display","block");//让文字显示出来。    $("#confirmS").css("display","none");    if(msg==""){        $("#confirmMsg").css("display","none");        $("#confirmS").css("display","block");          }    return msg == "";}$(function(){    var code;    $.post("../CheckCode",function(data){        code=data;    });    $("#re").click(function(){        $("#text1").css("display","block");        $("#text2").css("display","block");        $("#text3").css("display","block");        $("#text4").css("display","block");        $("#emailMsg").css("display","none");        $("#usernameMsg").css("display","none");        $("#passwordMsg").css("display","none");        $("#confirmMsg").css("display","none");        $("#yuanMsg").css("display","none");        $("#emailS").css("display","none");        $("#usernameS").css("display","none");        $("#passwordS").css("display","none");        $("#confirmS").css("display","none");        $("#yuanS").css("display","none");    });    $("#email").keyup(function(){        checkEmail();    });    $("#yuan").keyup(function(){        checkYuan();    });    $("#username").keyup(function(){        checkUsername();    });    $("#password").keyup(function(){        checkPassword();    });    $("#repassword").keyup(function(){        checkConfirm();    });    $("form").submit(function(){        if($("#yuan").val()==null||$("#yuan").val()==''){            return checkEmail()&&checkUsername();        }else{            return checkEmail()&&checkUsername()&&checkPassword()&&checkConfirm()&&checkYuan();        }    });});

5.ChangeInfoServlet

package cn.edu.bdu.mc.servlets;import java.io.IOException;import java.sql.SQLException;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.Cookie;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;import cn.edu.bdu.mc.beans.User;import cn.edu.bdu.mc.daos.UserDao;import cn.edu.bdu.mc.daos.impls.UserDaoImpl;import cn.edu.bdu.mc.utils.CookieUtil;/** * Servlet implementation class ChangeInfoServlet */@WebServlet("/ChangeInfo")public class ChangeInfoServlet extends HttpServlet {    private static final long serialVersionUID = 1L;           /**     * @see HttpServlet#HttpServlet()     */    public ChangeInfoServlet() {        super();        // TODO Auto-generated constructor stub    }    /**     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)     */    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {                try {            HttpSession session = request.getSession();            User user = (User) session.getAttribute("user");            String username = request.getParameter("username");            String email = request.getParameter("email");            String password = request.getParameter("password");            if(password==""||password==null) {                password = user.getPassword();            }            user.setUsername(username);            user.setPassword(password);            user.setEmail(email);            UserDao userDao = new UserDaoImpl();            userDao.update(user);            Cookie[] cookies = request.getCookies();            Cookie cookie = CookieUtil.findCookieByName(cookies, "user");            cookie.setValue(username+"#"+password);            response.sendRedirect(request.getContextPath()+"/client/user.jsp");        } catch (SQLException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }    }    /**     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)     */    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {        // TODO Auto-generated method stub        doGet(request, response);    }}

6.ShouhuoServlet

package cn.edu.bdu.mc.servlets;import java.io.IOException;import java.sql.SQLException;import java.util.HashMap;import java.util.Map;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import cn.edu.bdu.mc.beans.Shouhuo;import cn.edu.bdu.mc.beans.User;import cn.edu.bdu.mc.daos.impls.ShouhuoDaoImpl;import cn.edu.bdu.mc.services.impls.ShouhuoServiceImpl;/** * Servlet implementation class ShouHuoServlet */@WebServlet("/ShouHuo")public class ShouHuoServlet extends HttpServlet {    private static final long serialVersionUID = 1L;           /**     * @see HttpServlet#HttpServlet()     */    public ShouHuoServlet() {        super();        // TODO Auto-generated constructor stub    }    /**     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)     */    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {                String name = request.getParameter("name");        String phone = request.getParameter("phone");        String address = request.getParameter("address");        Shouhuo shouhuo = new Shouhuo();        User user = (User)request.getSession().getAttribute("user");        shouhuo.setUser_id(user.getUser_id());        shouhuo.setName(name);        shouhuo.setPhone(phone);        shouhuo.setAddress(address);        try {            new ShouhuoServiceImpl().addNewShouhuo(shouhuo);        } catch (SQLException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }        response.sendRedirect(request.getContextPath()+"/client/index.jsp");    }    /**     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)     */    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {        // TODO Auto-generated method stub        doGet(request, response);    }}

7.ShouhuoChangeServlet

package cn.edu.bdu.mc.servlets;import java.io.IOException;import java.sql.SQLException;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import cn.edu.bdu.mc.beans.Shouhuo;import cn.edu.bdu.mc.daos.impls.ShouhuoDaoImpl;import cn.edu.bdu.mc.services.ShouhuoService;import cn.edu.bdu.mc.services.impls.ShouhuoServiceImpl;/** * Servlet implementation class ShouhuoChangeServlet */@WebServlet("/ShouhuoChange")public class ShouhuoChangeServlet extends HttpServlet {    private static final long serialVersionUID = 1L;           /**     * @see HttpServlet#HttpServlet()     */    public ShouhuoChangeServlet() {        super();        // TODO Auto-generated constructor stub    }    /**     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)     */    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {                try {            String shouhuo_id = request.getParameter("shouhuo_id");            Shouhuo shouhuo = new ShouhuoDaoImpl().findById(Integer.parseInt(shouhuo_id));            ShouhuoService shouhuoService = new ShouhuoServiceImpl();            shouhuoService.changeDefault(shouhuo);            response.sendRedirect(request.getContextPath()+"/client/shou.jsp");        } catch (NumberFormatException | SQLException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }            }    /**     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)     */    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {        // TODO Auto-generated method stub        doGet(request, response);    }}

8.order.jsp

<%@page import="cn.edu.bdu.mc.beans.User"%><%@page import="cn.edu.bdu.mc.services.impls.OrderServiceImpl"%><%@page import="cn.edu.bdu.mc.services.OrderService"%><%@page import="cn.edu.bdu.mc.beans.Order"%><%@page import="java.util.List"%><%@ page language="java" contentType="text/html; charset=utf-8"    pageEncoding="utf-8"%>订单<%    User user = (User)request.getSession().getAttribute("user");    if(user==null){        response.sendRedirect(request.getContextPath()+"/client/login.jsp");    }else{    OrderService orderService = new OrderServiceImpl();    List
notPay = orderService.findNotPayByUser_id(user.getUser_id()); List
areadyPay = orderService.findAreadyPayByUser_id(user.getUser_id()); %>

未付款:

<% if(notPay.isEmpty()){ %> 无 <% }else{ %>
<% for(Order order : notPay){%>
<% } %> <% } %>
姓名 电话 地址 总价 创建时间 操作
查看详情 <%=order.getReciver_name() %> <%=order.getReciver_phone() %> <%=order.getReciver_address() %> <%=order.getMoney() %> <%=order.getOrder_time() %> 付款| 删除

已付款:

<% if(areadyPay.isEmpty()){ %> 无
<% }else{ %>
<% for(Order order : areadyPay){%>
<% } %> <% } %>
姓名 电话 地址 总价 创建时间
查看详情 <%=order.getReciver_name() %> <%=order.getReciver_phone() %> <%=order.getReciver_address() %> <%=order.getMoney() %> <%=order.getOrder_time() %>
<%} %>

9.orderitem.jsp

<%@page import="cn.edu.bdu.mc.beans.Book"%><%@page import="cn.edu.bdu.mc.daos.BookDao"%><%@page import="cn.edu.bdu.mc.daos.impls.BookDaoImpl"%><%@page import="org.apache.jasper.tagplugins.jstl.core.ForEach"%><%@page import="cn.edu.bdu.mc.daos.impls.OrderitemDaoImpl"%><%@page import="cn.edu.bdu.mc.daos.OrderitemDao"%><%@page import="cn.edu.bdu.mc.beans.Orderitem"%><%@page import="java.util.List"%><%@ page language="java" contentType="text/html; charset=utf-8"    pageEncoding="utf-8"%>订单详情
<% String order_id = request.getParameter("order_id"); OrderitemDao orderitemDao = new OrderitemDaoImpl(); BookDao bookDao = new BookDaoImpl(); List
items = orderitemDao.findItemById(order_id); %>

订单详情

<% for(Orderitem orderitem:items){ Book book = bookDao.findBookById(orderitem.getBook_id()); %>
<% }; %>
书名 价格 类别 描述
<%=book.getBook_name() %> <%=book.getPrice() %> 元 <%=book.getClazz() %> <%=book.getDescribtion() %>

10.cart.jsp

<%@page import="java.util.List"%><%@page import="cn.edu.bdu.mc.beans.Book"%><%@ page language="java" contentType="text/html; charset=utf-8"    pageEncoding="utf-8"%>空白
<% List
cart = (List
)request.getSession().getAttribute("cart"); if(cart==null){ out.print("这里竟然是空的"); }else{ %>
<% int i=1; for(Book book:cart){ if(i>15){break;}%>
价格 类别 数量 点击量 购买量 热度 描述 选中
<%=book.getPrice() %> 元 <%=book.getClazz() %> <%=book.getCount() %> <%=book.getClick_num() %> <%=book.getBuy_num() %> <%=book.getRe_du() %> <%=book.getDescribtion() %>
<% }; %>
<% }%>

11.NewOrderServlet中购物车的修改

HttpSession session = request.getSession();        List
cart = (List
) session.getAttribute("cart"); if(cart!=null) { List
need_del = new ArrayList
(); BookDao bookDao = new BookDaoImpl(); for(String book_id:book_ids) { for (Book book : cart) { if(book.getBook_id()==Integer.parseInt(book_id)) { need_del.add(book); } } } for (Book book : need_del) { cart.remove(book); } session.setAttribute("cart", cart);

购物车加订单效果展示

1513080-20190624235802094-616902021.png

1513080-20190624235818288-1794141778.png

1513080-20190624235831927-1479868594.png

转载于:https://www.cnblogs.com/zhangA/p/11080464.html

你可能感兴趣的文章
Ubuntu下的eclipse安装subclipse遇到没有javahl的问题...(2天解决了)
查看>>
alter database databasename set single_user with rollback IMMEDIATE 不成功问题
查看>>
Repeater + Resources 列表 [原创][分享]
查看>>
WCF揭秘——使用AJAX+WCF服务进行页面开发
查看>>
【题解】青蛙的约会
查看>>
IO流
查看>>
mybatis调用存储过程,获取返回的游标
查看>>
设计模式之装饰模式(结构型)
查看>>
面向对象的设计原则
查看>>
Swift3.0服务端开发(三) Mustache页面模板与日志记录
查看>>
【转】 FPGA设计的四种常用思想与技巧
查看>>
EntityFrameWork 实现实体类和DBContext分离在不同类库
查看>>
新手算法学习之路----二叉树(在一个二叉查找树中插入一个节点)
查看>>
autopep8
查看>>
GIT在Linux上的安装和使用简介
查看>>
基于C#编程语言的Mysql常用操作
查看>>
s3c2440实验---定时器
查看>>
MyEclipse10安装SVN插件
查看>>
[转]: 视图和表的区别和联系
查看>>
Regular Experssion
查看>>